package Tool;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import standard.Standard;

public class Client implements Linda{
	private Socket socket;
	public Client()
	{
		try{
			socket=new Socket("127.0.0.1",8000);
		}catch(IOException e){
			e.printStackTrace();
		}		
	}
	
	public void out(Object object,String name)
	{ 
		try{
			ByteArrayOutputStream ByteForObject=new ByteArrayOutputStream();
			ObjectOutputStream outStream=new ObjectOutputStream(ByteForObject);
			outStream.writeObject(object);
			byte[] data=ByteForObject.toByteArray();
			
			Standard standard=new Standard("out",name,data);	
			ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
			oos.writeObject(standard);
		}catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void out(String name, Object...objects)
	{
//		for(int float_index = 0;float_index<floats.length;float_index++)
//		{
			try
			{
				ByteArrayOutputStream ByteForObject = new ByteArrayOutputStream(); //catch the byte data
				ObjectOutputStream outStream = new ObjectOutputStream(ByteForObject); //means after serialization where to keep the serialized data
				outStream.writeObject(objects);
				byte[] data = ByteForObject.toByteArray();
				
				Standard standard = new Standard("out",name,data);
				ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
				oos.writeObject(standard);
			}catch(IOException e){
				e.printStackTrace();
			}
//		}
	}
	
	public Object in(String name)
	{
		try{
			Standard standard=new Standard("in",name);
			ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
			oos.writeObject(standard);
			
			InputStream is=socket.getInputStream();
			byte[] s=new byte[1024];
			int len2=is.read(s);
			ByteArrayInputStream TransferBackToS=new ByteArrayInputStream(s);
		    ObjectInputStream in2=new ObjectInputStream(TransferBackToS);
			Object object=(Object)in2.readObject();
			return object;
		}catch(IOException e){
			e.printStackTrace();
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}
		
		return null;
	}
	
	public Object read(String name)
	{
		try{
			Standard standard=new Standard("read",name);
			ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
			oos.writeObject(standard);
			
			InputStream is=socket.getInputStream();
			byte[] s=new byte[1024];
			int len2=is.read(s);
			ByteArrayInputStream TransferBackToS=new ByteArrayInputStream(s);
		    ObjectInputStream in2=new ObjectInputStream(TransferBackToS);
			Object object=(Object)in2.readObject();
			return object;
		}catch(IOException e){
			e.printStackTrace();
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}
		
		return null;
	}
	
	public void close()
	{
		try{
			this.socket.close();
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}
}
